@extends('frontend/layouts/flaty_to_bootstrap')

@section('navbar.inner2')
@show

@section('content.title')
<h1><i class="icon-list"></i> Summary</h1>
@stop

@section('content.breadcrumb.inner')
<li><a href="{{ route('home') }}"><i class="icon-home"></i> Dashboard</a><span class="divider"><i class="icon-angle-right"></i></span></li>
<li class="active">Summary</li>
@stop

@section('content.main')

<form action="{{ route('car_balance.main.summary')}}" method="post">
{{ Form::token() }}
<div id="toolbar" style="padding: 4px; border: 1px solid silver; border-radius: 3px"></div>
</form>

<div id="tab-group">
    <div id="tabs" style="width: 100%;"></div>
    <div id="tab-service" class="tab" style="width: 100%; height: 400px;"> </div>
    <div id="tab-month" class="tab" style="width: 100%; height: 400px;"> </div>
    <div id="tab-customer" class="tab" style="width: 100%; height: 400px;display: none;"> </div>
    <div id="tab-auction" class="tab" style="width: 100%; height: 400px;display: none;"> </div>
    <div id="tab-maker" class="tab" style="width: 100%; height: 400px;display: none;"> </div>
    <div id="tab-name" class="tab" style="width: 100%; height: 400px;display: none;"> </div>
</div>
<br>
<div id="grid-car" style="width: 100%; height: 500px;"></div>
@stop

@section('javascripts.common')
<script type="text/javascript" src="{{ asset('assets/jquery/jquery-2.1.1.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/bootstrap/bootstrap.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/w2ui/dist/w2ui.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/bootstrap-datepicker/moment.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/print/printThis.js') }}"></script>
<script type="text/javascript" src="{{ asset('assets/nimo/nimo_w2utils.js') }}"></script>
@stop

@section('stylesheets.main')
<link href="{{ asset('assets/font-awesome/4_2/css/font-awesome.css') }}" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="{{ asset('assets/w2ui/dist/w2ui.min.css') }}" />
<link href="{{ asset('assets/bootstrap/bootstrap.min.css') }}" rel="stylesheet">
<style>
input.tdate {
border:1px solid silver;
width:70px;
}
.ship-nothing {
color: blue;
}
.ship-before {
color: black;
}
.ship-time {
color: gray;
}
.ship-after {
color: red;
}
</style>
@stop

@section('javascripts.more')
<script type="text/javascript">
$(function () {
    var id = 1;
    var summary = {{ json_encode($summaries) }};
    function create_records2(obj) {
       var records = [];
       for (var prop in obj) {
          if(!obj.hasOwnProperty(prop)){
            continue;
          }
          var d = obj[prop];
          d.name = prop;
          d.recid = id++;
          d.gross_margin= d.bill_total_amount-d.payment_total_amount;
          records.push(d);
       };
       return records;
    }

    var cs = {
        balance_date_enable : function(w2,c, b){
           w2.get('balance').caption = c;
           $('input[name=balance]').val(c);
           if (b) {
                w2.enable('moveleft','moveright');
                $('input[name=dateE]').attr('readonly', true);
           } else {
                w2.disable('moveleft','moveright');
                $('input[name=dateE]').attr('readonly', false);
           }
           w2.refresh('balance');
        },
        balance_date_reset : function(obj,e,first){
           var t = $('input[name=balance]').val();
           if (t=='C') { return};
           var d = moment($('input[name=dateS]').val(), 'YYYY-MM-DD');
           if (!d || !d.isValid()) return;
           var n = first ? moment(d).startOf('month') : moment(d).endOf('month');
           //if(d.isSame(n)) {return;};
           this.balance_date_move(null, 0);
        },
        balance_date_move : function(w2,m){
           var d = moment($('input[name=dateS]').val(), 'YYYY-MM-DD');
           if (!d || !d.isValid()) return;
           var t = $('input[name=balance]').val();
           var p = (t=='Y') ? 11 : ((t=='Q') ? 2 : 0);
           m = (t=='Y') ? m*12 : m;
           $('input[name=dateS]').val(d.add(m, 'months').startOf('month').format('YYYY-MM-DD'));
           $('input[name=dateE]').val(d.add(p, 'months').endOf('month').format('YYYY-MM-DD'));
        },
        tabs: {
           name: 'tabs',
           active: 'tab-all',
           tabs: [
               { id: 'tab-service', caption: 'Services' },
               { id: 'tab-month', caption: 'Month' },
               { id: 'tab-customer', caption: 'Customers' },
               { id: 'tab-auction', caption: 'Auctions' },
               { id: 'tab-maker', caption: 'Makers' },
               { id: 'tab-name', caption: 'Car Name' },
           ],
           onClick: function (event) {
               $('#tab-group .tab').hide();
               $('#tab-group #' + event.target).show();
               var name = 'grid-'+event.target.replace('tab-', '');
               w2ui[name].refresh();
           }
        },
        grid: {
           default: {
               show: { lineNumbers : true, toolbar: true },
               columnGroups: [
                   { caption: '全体', span: 6 },
                   { caption: '仕入', span: 12 },
                   { caption: '請求', span: 8 },
               ],
               columns: [
                   { field: 'name', caption: '名称',  size: '80px', sortable: true, resizable: true},
                   { field: 'count', caption: '台数',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_total_amount', caption: '売上高',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'payment_total_amount', caption: '仕入高',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'gross_margin', caption: '売上総利益',  size: '80px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      //var val = this.getCellValue(ind, col_ind, summary); //
                      return '<span style="color:' + (record.gross_margin>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(record.gross_margin) + '</span>';
                   }},
                   { field: 'returned_recycle', caption: 'R返戻額',  size: '80px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      return '<span style="color:' + (record.returned_recycle>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(record.returned_recycle) + '</span>';
                   }},

                   { field: 'purchase_price', caption: '購入額',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'purchase_price_tax', caption: '消費税',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'car_tax_amount', caption: '自動車税',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'recycle_amount', caption: 'R料',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'auction_commission', caption: '落札料',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'vanning', caption: 'Vanning',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'transfer_fee', caption: '陸送',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'others', caption: 'その他',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'tic_send_fee', caption: '送料',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'drayage', caption: 'Drayage',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'customs', caption: '税関',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bl_fee', caption: 'BL',  size: '80px', render: 'int', sortable: true, resizable: true},

                   { field: 'bill_price', caption: '購入額',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_price_tax', caption: '消費税',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_commission', caption: '手数料',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_recycle', caption: 'R料',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_rakusatu', caption: '落札料',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_vanning', caption: 'Vanning',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_transport_fee', caption: '陸送',  size: '80px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_others', caption: 'その他',  size: '80px', render: 'int', sortable: true, resizable: true},
               ],
               toolbar: {
                   items: [
                       { type: 'break' },
                       { type: 'button', id: 'pay', caption: '仕入', icon: 'fa fa-minus-square' },
                       { type: 'button', id: 'bill', caption: '請求', icon: 'fa fa-plus-square' },
                       { type: 'break' },
                       { type: 'button', id: 'print', caption: '印刷', icon: 'fa fa-print' },
                       { type: 'button', id: 'csv', caption: 'CSV', icon: 'fa fa-file-text-o' },

                   ],
                   onClick: function (event) {
                       name = this.name.replace('_toolbar', '');
                       switch (event.target) {
                           case 'bill': w2ui[name].toggleColumn('purchase_price','purchase_price_tax','car_tax_amount','recycle_amount','auction_commission','vanning','transfer_fee','others','tic_send_fee'); break;
                           case 'pay': w2ui[name].toggleColumn('bill_price','bill_price_tax','bill_commission','bill_recycle','bill_rakusatu','bill_vanning','bill_transport_fee','bill_others');break;
                           case 'print': nimo_w2utils.gridPrint(w2ui[name]);break;
                           case 'csv': nimo_w2utils.grid2csv(w2ui[name]);break;

                       }
                   },
               },
           },
           car: {
               show: { header:true,footer:true,lineNumbers:true,toolbar:true },
               header: 'CARS',
               columnGroups: [
                   { caption: '全体', span: 16 },
                   { caption: '仕入', span: 10 },
                   { caption: '請求', span: 9 },
               ],
               multiSearch: true,
               searches: [
                   { field: 'purchase_date', caption: '日付', type: 'text' },
                   { field: 'auction_name', caption: '会場', type: 'text' },
                   { field: 'customer_id', caption: '顧客', type: 'text' },
                   { field: 'bill_total_amount', caption: '請求', type: 'int' },
                   { field: 'payment_total_amount', caption: '仕入', type: 'int' },
                   { field: 'gross_margin_nmh', caption: '利益算', type: 'int' },
                   { field: 'return_vat_nmh', caption: 'VAT', type: 'int' },
                   { field: 'return_recycle_nmh', caption: 'R返戻額', type: 'int' },
                   { field: 'return_car_tax', caption: '自動車税解約', type: 'int' },
                   { field: 'car_liability_insurance_cancel_amount', caption: '自賠責保険解約', type: 'int' }
               ],
               columns: [
                   { field: 'id', caption: 'ID',  size: '40px', sortable: true, resizable: true, render: function (record, index, column_index) {
                      return '<a href="{{url('car_balance/car')}}/'+record.id+'/show" target=_car class="'+(record.ship ? record.ship.replace(".","-") : '')+'" title="ship '+record.ship_ymd+'">'+record.id+'</a>';
                   }},
                   { field: 'purchase_date', caption: '日付',  size: '80px', sortable: true, resizable: true},
                   { field: 'auction_name', caption: '会場',  size: '80px', sortable: true, resizable: true},
                   { field: 'customer_id', caption: '顧客',  size: '40px', sortable: true, resizable: true},
                   { field: 'car_name', caption: '車名',  size: '40px', sortable: true, resizable: true},
                   { field: 'bill_total_amount', caption: '請求',  size: '70px', render: 'int', sortable: true, resizable: true},
                   { field: 'payment_total_amount', caption: '仕入',  size: '70px', render: 'int', sortable: true, resizable: true},
                   { field: 'gross_margin_nmh', caption: '利益算',  size: '60px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      return '<span style="color:' + (record.gross_margin_nmh>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(record.gross_margin_nmh) + '</span>';
                   }},
                   { field: 'return_vat_nmh', caption: 'VAT',  size: '60px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      val = record.return_vat_nmh ? record.return_vat_nmh : 0;
                      return '<span style="color:' + (val>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(val) + '</span>';
                   }},
                   { field: 'return_recycle_nmh', caption: 'R返戻額',  size: '80px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      val = record.return_recycle_nmh ? record.return_recycle_nmh : 0;
                      return '<span style="color:' + (val>0?'blue':'red') +';text-align: right;">' + '<a href="{{url('car_balance/main/document-car')}}/'+record.id+'/status" target=_status title="'+ val +'">'+ w2utils.formatNumber(val) +'</a>' + '</span>';
                   }},
                   { field: 'return_car_tax', caption: '自動車税解約',  size: '60px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      val = record.return_car_tax ? record.return_car_tax : 0;
                      return '<span style="color:' + (val>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(val) + '</span>';
                   }},
                   { field: 'car_liability_insurance_cancel_amount', caption: '自賠責保険解約',  size: '60px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      val = record.return_liability ? record.return_liability : 0;
                      return '<span style="color:' + (val>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(val) + '</span>';
                   }},
                   { field: 'tic_total', caption: 'TIC',  size: '60px',style:'text-align: right;',sortable: true, resizable: true, render: function (record, index, column_index) {
                      val = record.tic_total ? record.tic_total : 0;
                      return '<span style="color:' + (val>0?'blue':'red') +';text-align: right;">' + w2utils.formatNumber(val) + '</span>';
                   }},
                   { field: 'purchase_price', caption: '購入額',  size: '60px', render: 'int', sortable: true, resizable: true},
                   { field: 'vat_percent', caption: '税',  size: '15px', render: 'int', sortable: true, resizable: true},
                   { field: 'purchase_price_tax', caption: '消費税',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'car_tax_amount', caption: '自動車税',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'recycle_amount', caption: 'R料',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'auction_commission', caption: '落札料',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'vanning', caption: 'Vanning',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'transfer_fee', caption: '陸送',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'others', caption: 'その他',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'tic_send_fee', caption: '送料',  size: '40px', render: 'int', sortable: true, resizable: true},
                   { field: 'drayage', caption: 'Drayage',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'customs', caption: '税関',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bl_fee', caption: 'BL',  size: '50px', render: 'int', sortable: true, resizable: true},

                   { field: 'bill_price', caption: '購入額',  size: '60px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_vat', caption: '税',  size: '15px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_price_tax', caption: '消費税',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_commission', caption: '手数料',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_recycle', caption: 'R料',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_rakusatu', caption: '落札料',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_vanning', caption: 'Vanning',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_transport_fee', caption: '陸送',  size: '50px', render: 'int', sortable: true, resizable: true},
                   { field: 'bill_others', caption: 'その他',  size: '40px', render: 'int', sortable: true, resizable: true},
               ],
               toolbar: {
                   items: [
                       { type: 'button', id: 'calc', caption: '再計算', icon: 'fa fa-calculator' },
                       { type: 'break' },
                       { type: 'button', id: 'pay', caption: '仕入', icon: 'fa fa-minus-square' },
                       { type: 'button', id: 'bill', caption: '請求', icon: 'fa fa-plus-square' },
                       { type: 'break' },
                       { type: 'button', id: 'print', caption: '印刷', icon: 'fa fa-print' },
                       { type: 'button', id: 'csv', caption: 'CSV', icon: 'fa fa-file-text-o' },

                   ],
                   onClick: function (event) {
                       name = this.name.replace('_toolbar', '');
                       switch (event.target) {
                           case 'bill': w2ui[name].toggleColumn('purchase_price','purchase_price_tax','car_tax_amount','recycle_amount','auction_commission','vanning','transfer_fee','others','tic_send_fee'); break;
                           case 'pay': w2ui[name].toggleColumn('bill_price','bill_price_tax','bill_commission','bill_recycle','bill_rakusatu','bill_vanning','bill_transport_fee','bill_others');break;
                           case 'print': nimo_w2utils.gridPrint(w2ui[name]);break;;break;
                           case 'csv': nimo_w2utils.grid2csv(w2ui[name]);break;
                           case 'calc': nimo_w2utils.gridSummary(w2ui[name]);break;

                       }
                   },
               },
           },

        },
        obj : null
    };

    w2utils.settings.date_format='yyyy-mm-dd';
    $('#toolbar').w2toolbar({
        name: 'toolbar',
        items: [
            { type: 'menu',  id: 'balance', caption: '{{$filters['balance']}}', icon: 'fa fa-calendar',
                items: [
                    { text: 'Month', img: 'icon-page' },
                    { text: 'Quarter', img: 'icon-page' },
                    { text: 'Year', img: 'icon-page' },
                    { text: 'Custom', img: 'icon-page' }
                ]
            },
            { type: 'button', id: 'moveleft', caption: '', icon: 'fa fa-arrow-left' },
            { type: 'html',  id: 'date',html: '<div style="padding: 3px 10px;"><input class="tdate" name="dateS" value="{{$filters['purchase_date'][0]}}">-<input class="tdate" name="dateE" value="{{$filters['purchase_date'][1]}}"><input type="hidden" name="balance" value="{{$filters['balance']}}"></div>'},
            { type: 'button', id: 'moveright', caption: '', icon: 'fa fa-arrow-right' },
            { type: 'break',  id: 'break0' },
            { type: 'html',  id: 'pos', html: '<div style="padding: 3px 10px;"> <span class="fa fa-car"></span> {{ Form::select('service_company_id', $service_company_list, $filters['service_company_id'], ['style'=> 'width: 60px;']) }} </div>'},
            { type: 'html',  id: 'customer', html: '<div style="padding: 3px 10px;"> <span class="fa fa-user"></span> {{ Form::select('customer_id', $customer_list, $filters['customer_id'], ['style'=> 'width: 90px;']) }} </div>'},
            { type: 'break',  id: 'break0' },
            { type: 'spacer' },
            { type: 'html',  id: 'item6',html: '<input type="submit" value="APPLY" class="btn btn-primary">'},
        ]
    });
    w2ui.toolbar.on('*', function (event) {
        switch(event.target){
            case 'balance:Month':
            case 'balance:Quarter':
            case 'balance:Year':
                cs.balance_date_enable(this, event.target.substr(8,1),true);
                break;
            case 'balance:Custom':
                cs.balance_date_enable(this, event.target.substr(8,1),false);
                break;
            case 'moveleft':
                cs.balance_date_move(this, -1);
                break;
            case 'moveright':
                cs.balance_date_move(this, 1);
                break;
        }
    });
    $('input[name=dateS]').w2field('date', { }).on('change', function (event) {cs.balance_date_reset($(this), event,true);});
    $('input[name=dateE]').w2field('date', { start: $('input[name=dateS]') }).attr('readonly',true).on('change', function (event) {cs.balance_date_reset($(this), event,false);});

    $('#tabs').w2tabs(cs.tabs);
    $('#tab-service').w2grid($.extend({}, cs.grid.default, {name: 'grid-service',records: create_records2(summary.service)}));
    $('#tab-month').w2grid($.extend({}, cs.grid.default, {name: 'grid-month',records: create_records2(summary.month)}));
    $('#tab-customer').w2grid($.extend({}, cs.grid.default, {name: 'grid-customer',records: create_records2(summary.customer)}));
    $('#tab-auction').w2grid($.extend({}, cs.grid.default, {name: 'grid-auction',records: create_records2(summary.auction)}));
    $('#tab-maker').w2grid($.extend({}, cs.grid.default, {name: 'grid-maker',records: create_records2(summary.maker)}));
    $('#tab-name').w2grid($.extend({}, cs.grid.default, {name: 'grid-name',records: create_records2(summary.name)}));

    var records = create_records2(summary.car);
    records.push({ summary: true, recid: 'S-1', id:'SUM'});
    records.push({ summary: true, recid: 'S-2', id:'AVG'});
    $('#grid-car').w2grid($.extend({}, cs.grid.car, {name: 'grid-car',records: records}));
    w2ui.tabs.click('tab-service');

});
</script>
@stop
